package com.sunshine.lnuplus.model.room

import androidx.lifecycle.LiveData
import androidx.room.*

@Dao
interface TimetableDao {
    @Insert(onConflict = OnConflictStrategy.REPLACE)
    fun insertUser(vararg users: User?)

    @Insert(onConflict = OnConflictStrategy.REPLACE)
    fun insertLesson(vararg lessons: Lesson?)

    @Insert(onConflict = OnConflictStrategy.REPLACE)
    fun insertCountDown(vararg countDowns: CountDown?)

    @Update
    fun updateUser(vararg users: User?)

    @Update
    fun updateCountDown(vararg countDowns: CountDown?)

    @Delete
    fun deleteUser(vararg users: User?
    )

    @Delete
    fun deleteLesson(vararg lessons: Lesson?)

    @Query("DELETE FROM Lesson WHERE 1=1")
    fun deleteAllLesson()

    @Query("DELETE FROM COUNTDOWN WHERE lessonName = :lessonName")
    fun deleteCountDown(lessonName: String)

    @get:Query("SELECT * FROM User")
    val allLiveUser: LiveData<List<User?>?>?

    @get:Query("SELECT * FROM Lesson")
    val allLiveLesson: LiveData<List<Lesson?>?>?

    @get:Query("SELECT * FROM Lesson")
    val allLesson: List<Lesson>

    @get:Query("SELECT * FROM CountDown ORDER BY year,month,day,hour,minute")
    val allLiveCountDown: LiveData<List<CountDown?>?>?

    @get:Query("SELECT * FROM CountDown")
    val allCountDown: List<CountDown>

    @Query("SELECT * FROM CountDown WHERE year=:mYear and month=:mMonth and day=:mDay")
    fun getTodayCountDown(mYear: Int, mMonth: Int, mDay: Int) : List<CountDown>

    @Query("UPDATE lesson SET lessonName = :lessonName,dayOfWeek = :dayOfWeek,start = :start,`end` = :end,teacher = :teacher,location = :location,weeks=:weeks,color = :color where lessonName = :oldLessonName and dayOfWeek = :oldDayOfWeek and start = :oldStart and `end` = :oldEnd")
    fun updateLesson(
        oldLessonName: String,
        oldDayOfWeek: Int,
        oldStart: Int,
        oldEnd: Int,
        lessonName: String,
        dayOfWeek: Int,
        start: Int,
        end: Int,
        teacher: String,
        location: String,
        weeks: String,
        color: String)

    @Query("UPDATE lesson SET color = :backgroundColor WHERE lessonName = :lessonName")
    fun updateBackgroundColor(lessonName: String, backgroundColor: String)

    @Query("UPDATE CountDown SET lessonName = :lessonName WHERE lessonName = :oldLessonName")
    fun updateCountDownName(oldLessonName: String, lessonName: String)

    @Query("UPDATE CountDown SET location = :location WHERE lessonName = :lessonName")
    fun updateCountDownLocation(lessonName: String, location: String)

    @Query("UPDATE COUNTDOWN SET time = :time,year = :year,month = :month,day = :day,hour = :hour,minute = :minute WHERE lessonName = :lessonName")
    fun updateCountDownTime(lessonName: String, time: String, year: Int, month: Int, day: Int, hour: Int, minute: Int)
}